package com.ly.base.reflect;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import	java.util.Properties;

import com.ly.base.annotation.MyAnnotation;
import com.ly.base.annotation.SonAnnocation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.junit.jupiter.api.Test;

/**
 * @Classname Reflect
 * @Description TODO
 * @Date 2020/11/1 10:07
 * @Author 冷心影翼
 */

@MyAnnotation(value = "1", son = @SonAnnocation)
public class Reflect extends P<String>{
	private int a;

	@Data
	class U{
		private int a;

		public U(){

		}

		public U(int a) {
			this.a = a;
		}
	}

	@Test
	public void getClassWay() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
		Class<Reflect> r1 = Reflect.class;
		System.out.println(r1);
		//--------
		Reflect reflect = new Reflect();
		Class<? extends Reflect> r2 = reflect.getClass();
		System.out.println(r2);
		//--------
		Class<?> r3 = Class.forName("com.ly.base.reflect.Reflect");
		System.out.println(r3);
		//--------
		Class<?> r4 = U.class.getClassLoader().loadClass("com.ly.base.reflect.Reflect");
		System.out.println(r4);
	}

	@Test
	public void getProperties() throws IOException {
		Properties properties = new Properties();
		FileInputStream fis = new FileInputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\reflect\\init.properties");
		properties.load(fis);
		String name = properties.getProperty("name");
		System.out.println(name);
		//----------
		Properties properties2 = new Properties();
		InputStream resourceAsStream = Reflect.class.getClassLoader().getResourceAsStream("sinage.properties");
		properties2.load(resourceAsStream);
		String name1 = properties2.getProperty("info");
		System.out.println(name1);
	}


	@Test
	public void instance() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class<U> uClass = U.class;
		Constructor<U> constructor = uClass.getConstructor(Reflect.class);
		U u = constructor.newInstance(new Reflect());
		System.out.println(u.a);
		Constructor<U> constructor1 = uClass.getConstructor(Reflect.class,int.class);
		U u1 = constructor1.newInstance(new Reflect(),2);
		System.out.println(u1.a);
	}

	@Test
	public void method() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
		Class<Reflect> reflectClass = Reflect.class;
		Reflect reflect = reflectClass.newInstance();
		Method f1 = reflectClass.getMethod("f1", String.class);
		f1.invoke(reflect,"f1");
	}

	@Test
	public void annotation() throws NoSuchMethodException, IllegalAccessException, InstantiationException {
		Class<Reflect> reflectClass = Reflect.class;
		Reflect reflect = reflectClass.newInstance();
		Annotation[] annotations = reflectClass.getAnnotations();
		System.out.println(annotations[0]);
	}

	@Test
	public void getParentClass() throws NoSuchMethodException, IllegalAccessException, InstantiationException {
		Class<Reflect> reflectClass = Reflect.class;
		Reflect reflect = reflectClass.newInstance();
		Class<? super Reflect> superclass = reflectClass.getSuperclass();
		Object object = superclass.newInstance();
		System.out.println(object);
	}

	@Test
	public void getGenericParentClass() throws NoSuchMethodException, IllegalAccessException, InstantiationException {
		Class<Reflect> reflectClass = Reflect.class;
		Reflect reflect = reflectClass.newInstance();
		ParameterizedType genericSuperclass = (ParameterizedType) reflectClass.getGenericSuperclass();
		System.out.println(genericSuperclass);
		System.out.println(genericSuperclass.getActualTypeArguments()[0]);
	}


	public void f1(String s) {
		System.out.println(s);
	}
}
